<template>
	<el-form size="large" class="login-content-form">
		<el-form-item class="login-animation1">
			<el-input text :placeholder="$t('message.account.accountPlaceholder1')" v-model="state.ruleForm.username" clearable autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-User /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation2">
			<el-input
				:type="state.isShowPassword ? 'text' : 'password'"
				:placeholder="$t('message.account.accountPlaceholder2')"
				v-model="state.ruleForm.password"
				autocomplete="off"
			>
				<template #prefix>
					<el-icon class="el-input__icon"><ele-Unlock /></el-icon>
				</template>
				<template #suffix>
					<i
						class="iconfont el-input__icon login-content-password"
						:class="state.isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
						@click="state.isShowPassword = !state.isShowPassword"
					>
					</i>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation4">
			<el-button type="primary" class="login-content-submit" round v-waves @click="open" :loading="state.loading.signIn">
				<span>{{ $t('message.account.accountBtnText') }}</span>
			</el-button>
		</el-form-item>
	</el-form>

	<div>
		<Vcode :show="isShow" @success="onSuccess" @close="onClose" @fail="onFail"></Vcode>
	</div>
</template>

<script setup lang="ts" name="loginAccount">
import { reactive, ref } from 'vue';
import Cookies from 'js-cookie';
import { Session } from '/@/utils/storage';
import { useLoginApi } from '/@/api/login';
import bcryptjs from 'bcryptjs';
import Vcode from 'vue3-puzzle-vcode';
import {ElMessage} from "element-plus";
import { AxiosResponse } from "axios";

let isShow = ref(false);
// 定义变量内容
const emit = defineEmits(['signInSuccess']); // 声明事件名称
const state = reactive({
	isShowPassword: false,
	ruleForm: {
		username: 'mingyue',
		password: '123456'
	},
	loading: {
		signIn: false,
	},
	salt: '$2a$12$pBVti6NTcVJxptxHNqetBe',
});

// 滑块验证码 ==== start
function open() {
	isShow.value = true;
}
function onSuccess() {
	isShow.value = false;
	onSignIn();
}
function onFail() {
	isShow.value = false;
}
function onClose() {
	isShow.value = false;
}
// 滑块验证码 ==== end

// 登录
const onSignIn = async () => {
	state.loading.signIn = true;
  // 密码加密
	state.ruleForm.password = bcryptjs.hashSync(state.ruleForm.password, state.salt);

  try {
    useLoginApi()
        .signIn(state.ruleForm)
        .then(async (res: AxiosResponse<any>) => {
          if (res.code === 500) {
            ElMessage.error(res.msg);
            Session.clear();
            state.loading.signIn = false;
            return;
          }

          // 存储 token 到浏览器缓存
          Session.set('token', res.data);
          // 模拟数据，对接接口时，记得删除多余代码及对应依赖的引入。用于 `/src/stores/userInfo.ts` 中不同用户登录判断（模拟数据）
          Cookies.set('username', state.ruleForm.username);

          emit('signInSuccess');
        });
  } finally {
    // 登录结束
    state.loading.signIn = false;
  }

};
</script>

<style scoped lang="scss">
.login-content-form {
	margin-top: 20px;
	@for $i from 1 through 4 {
		.login-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}
	.login-content-password {
		display: inline-block;
		width: 20px;
		cursor: pointer;
		&:hover {
			color: #909399;
		}
	}
	.login-content-code {
		width: 100%;
		padding: 0;
		font-weight: bold;
		letter-spacing: 5px;
	}
	.login-content-submit {
		width: 100%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}
}
</style>
